<script setup lang='ts'>
import { ref, reactive,toRaw,onMounted } from 'vue'
import { createAuthPolicyDetail,updateAuthPolicyDetail } from "@/api/authorization";
import { getApplicationNameList,getAllcompany } from "@/api/empower";

const form = {
    authType: "",
    policyName: "",
    authPosition: "",
    applicationName: "",
    role: "",
    functionPermission: "",
    groupName: "",
    companyCode: "",
    companyCodeExcluded: "",
    riskCode: "",
    riskCodeExcluded: "",
    id: ""
}
  let roleOptions = ref([])
  let roleOptions2 = ref([])
  //获取应用系统
  const getgetApplicationNameList = async () => {
    let res = await getApplicationNameList();
    if (res.code == 200) {
      roleOptions.value = res.data;
    }
  };
    //获取机构
  const getgetAllcompany = async () => {
    let res = await getAllcompany()
    if (res.code == 200) {
      roleOptions2.value = res.data
    } else {
      roleOptions2.value = []
    }
  }

const addAuthorizationo = async () => {
    let formData = {
      "saaAuthorizationPoliciesVo" :toRaw(form)
    }

    if(formData.saaAuthorizationPoliciesVo.id){
      // 修改
        const { data } = await createAuthPolicyDetail(formData);
    }else{
      // 新增
      const { data } = await createAuthPolicyDetail(formData);
    }
}


  onMounted(async () => {
    getgetApplicationNameList()
    getgetAllcompany()
  });
</script>

<template>
  <div class="authorization">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      label-width="120px"
    >
      <div style="margin: 10px 0">
        授权策略新增/修改
      </div>
      <el-row>
        <el-col :span="24">
          <el-form-item label="授权类型：" prop="name">
            <el-select
              v-model="form.authType"
              placeholder="请选择状态"
              clearable
              class="!w-[180px]"
            >
              <el-option label="授权岗位" value="1" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="策略名称：" prop="name">
            <el-input
              v-model="form.policyName"
              placeholder="请输入角色名称"
              clearable
              class="!w-[180px]"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="授权岗位：" prop="name">
            <el-input
              v-model="form.authPosition"
              placeholder="请输入角色名称"
              clearable
              class="!w-[180px]"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="应用系统：" prop="name">
              <el-select
                v-model="form.applicationName"
                placeholder="请选择"
                clearable
                class="!w-[180px]"
              >
                <el-option
                  v-for="(item, index) in roleOptions"
                  :key="index"
                  :value="item.applicationCode"
                  :label="item.applicationName"
                >
                  {{ item.applicationName }}
                </el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="角色：" prop="name">
            <el-input
              v-model="form.role"
              placeholder="请输入角色名称"
              clearable
              class="!w-[180px]"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="功能权限：" prop="name">
            <el-input
              v-model="form.functionPermission"
              placeholder="请输入角色名称"
              clearable
              class="!w-[180px]"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="群组：" prop="name">
            <el-input
              v-model="form.groupName"
              placeholder="请输入角色名称"
              clearable
              class="!w-[180px]"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <div>数据权限</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="机构：" prop="name">
            <el-select
              v-model="form.companyCode"
              placeholder="请选择"
              clearable
              class="!w-[180px]"
            >
              <el-option
                v-for="(item, index) in roleOptions2"
                :key="index"
                :value="item.comCode"
                :label="item.comCname"
              >
                {{ item.comCname }}
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="是否除外：">
            <el-radio-group v-model="form.companyCodeExcluded">
              <el-radio value="1" >是</el-radio>
              <el-radio value="2" >否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="险种：" prop="name">
            <el-input
              v-model="form.riskCode"
              placeholder="请输入角色名称"
              clearable
              class="!w-[180px]"
            />
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="是否除外：">
            <el-radio-group v-model="form.riskCodeExcluded">
              <el-radio value="1" >是</el-radio>
              <el-radio value="2" >否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button
            type="primary"
            @click="addAuthorizationo"
          >
            提交
          </el-button>
          <el-button type="primary">
            取消
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
</template>

<style lang='scss' scoped>

</style>